<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/MemberDomain.php';
class MemberDAO extends BaseMySQLDAO {

	public function __construct($context) {
		parent::__construct($context, MemberDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO member(dwo,PIC,clientCompany,clientName,clientContact,location,email)
					VALUES	(
								".$obj->getDwo().",
								'".$obj->getPIC()."',
								'".$obj->getClientCompany()."',
								'".$obj->getClientName()."',
								'".$obj->getClientContact()."',
								'".$obj->getLocation()."',
								'".$obj->getEmail()."'
							)";
		echo $inSQL;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$oid = $this->getMySQLConnection()->mysqlLastInsertID();
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $oid;
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM member WHERE memberID=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE member
				SET
					member.dwo = ".$obj->getDwo().",
					member.PIC = '".$obj->getPIC()."',
					member.clientCompany = '".$obj->getClientCompany()."',
					member.clientName = '".$obj->getClientName()."',
					member.clientContact = '".$obj->getClientContact()."',
					member.location = '".$obj->getLocation()."'
					member.email = '".$obj->getEmail()."'
				 WHERE memberID=".$obj->getMemberID();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}
	
	public function findByOid($oid)
	{
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM member WHERE member.memberID=".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		$memberDomain = new MemberDomain($res);	
		return $memberDomain;
	}
	
	public function findByDwoNumber($dwoNumber)
	{
		$memberDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM member WHERE member.dwo=".$dwoNumber;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		if($res != false)
			$memberDomain = new MemberDomain($res);	
		return $memberDomain;
	}
	
	public function findByEmail($email)
	{
		$memberDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM member WHERE member.email='".$email."'";
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		if($res != false)
			$memberDomain = new MemberDomain($res);	
		return $memberDomain;
	}
	
	public function findAllMember ()
	{		
		$this->getContext()->getDBTransaction()->startTransaction();
		$memberDomainArr = array();
		$inSQL = "SELECT * FROM member";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		
		while ($data = mysql_fetch_assoc($res))
		{
			$memberDomain = new MemberDomain($data);
			$memberDomainArr[] = $memberDomain;
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $memberDomainArr;
	}
}
?>